unit uEmpenhadoConsulta;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai_new, Menus, DB, StdCtrls, Buttons, ExtCtrls, MMJPanel,
  FMTBcd, Grids, DBGrids, JvExDBGrids, JvDBGrid, DBClient, Provider,
  SqlExpr;

type
  TfEmpenhoConsulta = class(TfPai_new)
    sqlEmp: TSQLQuery;
    dspEmp: TDataSetProvider;
    cdsEmp: TClientDataSet;
    dsEmp: TDataSource;
    gridOf: TJvDBGrid;
    Label1: TLabel;
    Label2: TLabel;
    edMaterial: TEdit;
    edLote: TEdit;
    Label3: TLabel;
    edProduto: TEdit;
    cbStatus: TComboBox;
    Label4: TLabel;
    lblLog: TLabel;
    sspConfirma: TSQLStoredProc;
    cdsEmpTIPO: TStringField;
    cdsEmpETIQUETA: TStringField;
    cdsEmpLOTE: TFMTBCDField;
    cdsEmpCOMPONENTE: TStringField;
    cdsEmpDESCRICAO_COMPONENTE: TStringField;
    cdsEmpPRODUTO_DESTINO: TStringField;
    cdsEmpDESCRICAO_PRODUTO: TStringField;
    cdsEmpTIPO_DOC: TStringField;
    cdsEmpQTDE: TFMTBCDField;
    cdsEmpOFDET_NF: TStringField;
    procedure btnProcurarClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure edMaterialKeyPress(Sender: TObject; var Key: Char);
  private
    procedure abrirGrid;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fEmpenhoConsulta: TfEmpenhoConsulta;

implementation

uses udm;

{$R *.dfm}

{ TfPai_new1 }

procedure TfEmpenhoConsulta.abrirGrid;
var sql, sql1: string;
begin
  sql := 'SELECT * FROM ' +
     ' ( ' +
     ' SELECT ' + QuotedStr('CONFIRMACAO DE RECEBIMENTO') + ' TIPO, EOF_CODETI ' +
     ' ETIQUETA, ETI_LOTE LOTE, EOF_CDCOMP COMPONENTE, A.PRO_DESCRI ' +
     ' DESCRICAO_COMPONENTE, EOF_CODPRO PRODUTO_DESTINO, B.PRO_DESCRI ' +
     ' DESCRICAO_PRODUTO , DECODE(ETI_COMP19,NULL, ' + QuotedStr('NF') + ', ' +
     QuotedStr('OF') + ') TIPO_DOC, NVL(ETI_COMP19,ETI_NTFISC) OFDet_NF, ETI_QTDENV QTDE ' +
     ' FROM ATS_ETIQOF, F_ETIQUETAS, F_PRODS A, F_PRODS B ' +
     ' WHERE EOF_CODETI = ETI_CODETI ' +
     ' AND   A.PRO_CODPRO = EOF_CDCOMP ' +
     ' AND   B.PRO_CODPRO = EOF_CODPRO ' +
     ' AND   ETI_EATIVA = 1 ' +
     ' AND   EOF_STATUS NOT IN (0,3)' +
     ' UNION ALL ' +
     ' SELECT ' + QuotedStr('EMPENHO DE MATERIAIS NA O.F.') + ' TIPO,ETI_CODETI, ' +
     ' ETI_LOTE LOTE, ETI_CODPRO COMPONENTE, A.PRO_DESCRI DESCRICAO_COMPONENTE, ' +
     ' B.PRO_CODPRO PRODUTO_DESTINO, B.PRO_DESCRI DESCRICAO_PRODUTO , ' +
     ' DECODE(ETI_COMP19,NULL,' + QuotedStr('NF') + ', ' + QuotedStr('OF') +
     ' ) TIPO_DOC, NVL(ETI_COMP19,ETI_NTFISC) OFDet_NF, ETI_QTDENV QTDE ' +
     ' FROM F_ETIQUETAS, F_PRODS A, F_PRODS B ' +
     ' WHERE A.PRO_CODPRO = ETI_CODPRO ' +
     ' AND ' + QuotedStr('A DISTRIBUIR') + ' = B.PRO_CODPRO ' +
     ' AND   ETI_EATIVA=1 ' +
     ' AND   ETI_LOCEST = ' + QuotedStr('PROC') +
     ' AND   NOT EXISTS ' +
     '( ' +
     ' SELECT 1 FROM ATS_ETIQOF ' +
     ' WHERE EOF_CODETI = ETI_CODETI ' +
     '))';
  sql1 := '';
  if (edMaterial.Text <> '') then
  begin
    sql1 := sql1 + ' WHERE COMPONENTE = ' + QuotedStr(edMaterial.Text);
  end;
  if (edProduto.Text <> '') then
  begin
    if (sql1 = '') then
      sql1 := ' WHERE PRODUTO = ' + QuotedStr(edProduto.Text)
    else
      sql1 := sql1 + ' AND PRODUTO = ' + QuotedStr(edProduto.Text);
  end;
  if (edLote.Text <> '') then
  begin
    if (sql1 = '') then
      sql1 := ' WHERE LOTE = ' + QuotedStr(edLote.Text)
    else
      sql1 := sql1 + ' AND LOTE = ' + QuotedStr(edLote.Text);
  end;

  if (cbStatus.ItemIndex > 0) then
  begin
    if (sql1 = '') then
      sql1 := ' WHERE TIPO = ' + QuotedStr(cbStatus.Text)
    else
      sql1 := sql1 + ' AND TIPO = ' + QuotedStr(cbStatus.Text);
  end;


  if (cdsEmp.Active) then
    cdsEmp.close;
  cdsEmp.CommandText := sql + sql1;
  cdsEmp.open;


end;

procedure TfEmpenhoConsulta.btnProcurarClick(Sender: TObject);
begin
  //inherited;
  abrirGrid;
  if (cdsEmp.RecordCount > 0) then
    lblLog.Caption := 'Encontrado ' + IntToStr(cdsEmp.RecordCount) + ' registros.'
  else
    lblLog.Caption := 'Nao encontrado registros.';
end;

procedure TfEmpenhoConsulta.btnGravarClick(Sender: TObject);
var  Save_Cursor: TCursor;
Begin
  if  MessageDlg('Confirmar o recebimento destas ' + QuotedStr(IntToStr(cdsEmp.RecordCount)) + ' etiquetas ?',
     mtConfirmation, [mbYes, mbNo],0) = mrNo then exit;

  Save_Cursor := Screen.Cursor;
  try
    Screen.Cursor := crHourGlass;    { Show hourglass cursor }
    while not cdsEmp.Eof do
    begin
      if (sspConfirma.Active) then
        sspConfirma.Close;
      sspConfirma.ParamByName('CODETI').AsString  := cdsEmp.Fields[1].AsString;
      sspConfirma.ParamByName('USUARIO').AsString := dm.user;
      sspConfirma.ExecProc;
      sspConfirma.Close;
      cdsEmp.Next;
    end;
    MessageDlg('Recebimento confirmado.' , mtInformation, [mbOk],0);
    abrirGrid;
  finally
    Screen.Cursor := Save_Cursor;  { Always restore to normal }
  end;
end;

procedure TfEmpenhoConsulta.edMaterialKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (key = #13) then
  begin
    btnProcurar.Click;
  end;

end;

end.
